package com.leadnews.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leadnews.mapper.LevelMapper;

import com.leadnews.mapper.NovelContentMapper;
import com.leadnews.mapper.UserMapper;
import com.leadnews.pojo.Level;
import com.leadnews.pojo.Novel;
import com.leadnews.pojo.NovelContent;
import com.leadnews.pojo.User;
import com.leadnews.service.NovelContentService;
import com.leadnews.service.NovelService;
import com.leadnews.utils.SpringContextUtil;
import com.leadnews.vo.PageResult;
import com.leadnews.vo.ResponseResult;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.leadnews.utils.AppJwtUtil.getClaimsBody;

@RestController
@RequestMapping
@CrossOrigin(origins = "*",maxAge = 3600)
public class NovelController {
    @Autowired
    NovelContentService novelContentService;
    @Autowired
    NovelContentMapper novelContentMapper;
    @Autowired
    UserMapper userMapper;
    @PostMapping("/getNovel")
    public ResponseResult getNovel(@RequestBody Map<String,String> map){
        String token = map.get("name");
        String[] split = token.split("%");
        ResponseResult result = new ResponseResult();
        result.setCode(0);
        NovelContent novelContent=novelContentMapper.findByIdAndTitle(split[0],split[1]);

            if (novelContent==null){
                result.setErrorMessage("未找到相应章节");
            }else{

            result.setData(novelContent);
            result.setCode(200);
            }


        return result;
    }

    @PostMapping("/next")
    public ResponseResult next(@RequestParam("uid") String novelname1){
        ResponseResult result = new ResponseResult();
        result.setCode(0);
        String[] split = novelname1.split("Q");
        System.out.println(novelname1);
        NovelContent novelContent=novelContentMapper.findNext(split[0],split[1]);

        if (novelContent!=null){
            result.setData(novelContent);
            result.setCode(200);
        }else{
            result.setErrorMessage("未找到相应章节内容");
        }
        return result;
    }

    @Autowired
    NovelService novelService;
    @PostMapping("/getNovelName")
    public ResponseResult getNovelName(@RequestParam("novelname") String novelname){
        return novelService.getNovelName(novelname);
    }
    @GetMapping("/novel2")
    public ResponseResult getNovel(){
        return novelService.getNovel();
    }
    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("/test/addNovel")
    public ResponseResult addNovel(){
        ListOperations listOperations = redisTemplate.opsForList();
        LambdaQueryWrapper<NovelContent> queryWrapper = new LambdaQueryWrapper<>();
        List<NovelContent> novelContents = novelContentMapper.selectList(queryWrapper);
        for (int i = 0; i <novelContents.size(); i++) {

            listOperations.rightPush(novelContents.get(i).getNovelId() + "", novelContents.get(i).getTitle());
        }
        return ResponseResult.okResult(200,"添加小说成功");
    }
}
